/*******************************************************************************
**
** Copyright (c) 2011 - The OWE Team
**
** This file is part of OWE.
**
** OWE is a free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License as published by the Free Software Foundation; version
** version 2.1 of the License.
**
** OWE is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public
** License along with OWE. If not, see <http://www.gnu.org/licenses/>.
**
*******************************************************************************/

#include <tut/tut.hpp>
#include <tut/tut_console_reporter.hpp>

#include <owe/configurationdocument.h>
#include <owe/widgetinterface.h>
#include <owe/widgetpackage.h>
#include <owe/ustring.h>

#include "utests.h"

OWE_USE_NAMESPACE

namespace tut
{
    test_runner_singleton runner;

    struct WidgetInterface_data {
        WidgetInterface_data() {
            package = new WidgetPackage(TEST_DATA"/widget01/");
            configuration = new ConfigurationDocument(*package);
            interface = new WidgetInterface(*configuration);

            ensure(package->isValid() == true);
            ensure(configuration->isValid() == true);
            ensure(interface->isValid() == true);
        }

        ~WidgetInterface_data() {
            delete package;
            delete configuration;
            delete interface;
        }

        WidgetPackage *package;
        ConfigurationDocument *configuration;
        WidgetInterface *interface;
    };

    typedef test_group<WidgetInterface_data> testgroup;
    testgroup shared_ptr_group("WidgetInterface");
    typedef testgroup::object testobject;

    template<>
    template<>
    void testobject::test<1>() {
        set_test_name("UString WidgetInterface::author() const");

        ensure(interface->author() == "OWE");

        interface->setLocale("pt-br");
        ensure(interface->author() == "OWE");
    }

    template<>
    template<>
    void testobject::test<2>() {
        set_test_name("UString WidgetInterface::authorEmail() const");

        ensure(interface->authorEmail() == "owe@example.org");

        interface->setLocale("pt-br");
        ensure(interface->authorEmail() == "owe@example.org");
    }

    template<>
    template<>
    void testobject::test<3>() {
        set_test_name("UString WidgetInterface::authorHref() const");

        ensure(interface->authorHref() == "http://example.org/widget01");

        interface->setLocale("pt-br");
        ensure(interface->authorHref() == "http://example.org/widget01");
    }

    template<>
    template<>
    void testobject::test<4>() {
        set_test_name("UString WidgetInterface::description() const");

        UString description = "An example to demonstrate a valid config.xml";
        ensure(interface->description() == description);

        UString description_ptbr = "Um exemplo para demonstrar um config.xml válido";
        interface->setLocale("pt-br");
        ensure(interface->description() == description_ptbr);
    }

    template<>
    template<>
    void testobject::test<5>() {
        set_test_name("UString WidgetInterface::id() const");

        ensure(interface->id() == "http://example.org/widget01");

        interface->setLocale("pt-br");
        ensure(interface->id() == "http://example.org/widget01");
    }

    template<>
    template<>
    void testobject::test<6>() {
        set_test_name("UString WidgetInterface::name() const");

        // FIXME: Name must be normalized according to:
        // http://www.w3.org/TR/widgets/#rule-for-getting-text-content-with-norma0
        // ensure(interface->name() == "widget01: valid config.xml");
        //
        // interface->setLocale("pt-br");
        // ensure(interface->name() == "Widget01: config.xml válido");
    }

    template<>
    template<>
    void testobject::test<7>() {
        set_test_name("UString WidgetInterface::shortName() const");

        // FIXME: Short name must be retrieved according to:
        // http://www.w3.org/TR/widgets/#rule-for-getting-a-single-attribute-valu0
        ensure(interface->shortName() == "Widget01 - default language");

        interface->setLocale("pt-br");
        ensure(interface->shortName() == "Widget01 - português do Brasil");
    }

    template<>
    template<>
    void testobject::test<8>() {
        set_test_name("PreferenceElementList WidgetInterface::preferences() const");
    }

    template<>
    template<>
    void testobject::test<9>() {
        set_test_name("UString WidgetInterface::version() const");

        // FIXME: Version must be retrieved according to:
        // http://www.w3.org/TR/widgets/#rule-for-getting-a-single-attribute-valu0
        ensure(interface->version() == "1.0");

        interface->setLocale("pt-br");
        ensure(interface->version() == "1.0");
    }

    template<>
    template<>
    void testobject::test<10>() {
        set_test_name("unsigned long WidgetInterface::height() const");

        // FIXME: Height must be retrieved as numeric value according to:
        // http://www.w3.org/TR/widgets/#rule-for-parsing-a-non-negative-integer0
        ensure(interface->height() == "150");

        interface->setLocale("pt-br");
        ensure(interface->height() == "150");

    }

    template<>
    template<>
    void testobject::test<11>() {
        set_test_name("unsigned long WidgetInterface::width() const");

        // FIXME: Width must be retrieved as numeric value according to:
        // http://www.w3.org/TR/widgets/#rule-for-parsing-a-non-negative-integer0
        ensure(interface->width() == "150");

        interface->setLocale("pt-br");
        ensure(interface->width() == "150");
    }
}

int main(void)
{
    tut::console_reporter reporter;
    tut::runner.get().set_callback(&reporter);
    tut::runner.get().run_tests();

    return reporter.all_ok() ? 0 : 1;
}
